package fafmessenger.control;

import java.sql.Timestamp;
import java.util.ArrayList;
import android.content.Context;
import android.telephony.TelephonyManager;
import fafmessenger.entity.Contact;
import fafmessenger.entity.Message;
import fafmessenger.entity.Response;

public class InboxController {

	private static DataController dataController;
	private Communicator communicator;
	private Context context;
	
	public InboxController(Context context) {
		dataController = new DataController(context);
		communicator = new Communicator();
		this.context = context;
	}
	
	public DataController getDataController(){
		return dataController;
	}
	
	public void deleteAllMessages() {
		dataController.deleteAll();
	}
	
	public ArrayList<Contact> getAllContactsWithMessages() {
		return dataController.getContactsWithConversations();
	}
	
	public Contact getContactByMobilenumber(String mobilenumber){
		Contact contact = null;
		contact = dataController.getContactByMobilenumber(mobilenumber); 
		return contact;
	}
	
	public void deleteConversation(String mobilenumber){
		dataController.deleteConversation(mobilenumber);
	}
	
	public void goToConversation(Contact c) {
		
	}
	
	public Response register() {
		Response returnvalue = null;
		
		TelephonyManager tMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		String myMobilenumber = tMgr.getLine1Number();
		returnvalue = communicator.register(myMobilenumber);
		
		return returnvalue;
	}
	
	public Response sendMessage(String toMobilenumber, String body) {
		Response returnvalue = null;
		
		TelephonyManager tMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		String myMobilenumber = tMgr.getLine1Number();
		returnvalue = communicator.sendMessage(myMobilenumber, toMobilenumber, body);
		
		return returnvalue;
	}
	
	public boolean refresh() {
		boolean returnvalue = false;
		
		TelephonyManager tMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		String myMobilenumber = tMgr.getLine1Number();
		
		ArrayList<Message> messages = dataController.getMessages();
		
		String fromTimestamp = Timestamp.valueOf("0000-00-00 00:00:00.0").toString();
		
		if (messages.size() > 0) {
			Message message = messages.get(messages.size() - 1);
			fromTimestamp = message.getTimestamp().toString();
		}
		
		Object result = communicator.getMessages(myMobilenumber, fromTimestamp);
		
		if (result instanceof ArrayList<?>) {
			ArrayList<String[]> newMessages = (ArrayList<String[]>)result;
			
			if (newMessages.size() > 0) {
				for (int i = 0; i < newMessages.size(); i++) {
					String[] arr = newMessages.get(i);
					Message temp = new Message(Timestamp.valueOf(arr[2]), arr[3], arr[0], arr[1]);
					dataController.saveConversations(temp);
				}
				
				returnvalue = true;
			}
		}
		
		return returnvalue;
	}
	
	public void Search() {
		
	}
	
	public void newMessage() {
		
	}
}
